package paypal

import (
	"context"
	"github.com/gin-gonic/gin"
	model2 "simplifly-tech/payment/model"
)

type SendCode string

const (
	SendCodeAuth SendCode = "v1/oauth2/token"
)

type PaypalService struct {
	Host        string `json:"host"`
	ClientId    string `json:"clientId"`
	Secret      string `json:"secret"`
	AccessToken string `json:"access_token"`
	UnitTest    bool   `json:"unitTest"`
}

func (paypalService *PaypalService) RefundNotify(ctx *gin.Context) error {
	//TODO implement me
	panic("implement me")
}

func (paypalService *PaypalService) Notify(ctx *gin.Context) (*model2.PaymentNotification, error) {
	//TODO implement me
	panic("implement me")
}

func (paypalService *PaypalService) Refund(ctx context.Context, refundData *model2.RefundData) (string, error) {
	//TODO implement me
	panic("implement me")
}

func (paypalService *PaypalService) CancelPay(id string) error {
	//TODO implement me
	panic("implement me")
}

func (paypalService *PaypalService) Init() {
	accessToken, err := paypalService.getAccessToken()
	if err != nil {
		return
	}
	paypalService.AccessToken = accessToken
}

func (paypalService *PaypalService) getAccessToken() (string, error) {
	//if paypalService.UnitTest == false {
	//	cacheKey := cache.GetPayPalRedisAccessToken()
	//	accessToken, err := db.GetRedisClient().Get(context.Background(), cacheKey).Result()
	//	if err != nil && accessToken != "" {
	//		return accessToken, err
	//	}
	//}
	//client := &http.Client{
	//	Timeout: time.Second * 10,
	//}
	//paramsJson := `grant_type=client_credentials`
	//req, err := http.NewRequest("POST", paypalService.Host+"/"+string(SendCodeAuth), strings.NewReader(paramsJson))
	//if err != nil {
	//	log.Errorf(" (paypalService *PaypalService) Init()   req, err := http.NewRequest err %v", err)
	//	return "", err
	//}
	//req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	//req.SetBasicAuth(paypalService.ClientId, paypalService.Secret)
	//resp, err := client.Do(req)
	//if err != nil {
	//	log.Errorf(" (paypalService *PaypalService) Init()  err := client.Do(req)err %v", err)
	//	return "", err
	//}
	//defer resp.Body.Close()
	//body, err := ioutil.ReadAll(resp.Body)
	//if err != nil {
	//	log.Errorf(" (paypalService *PaypalService) Init() getResponse ioutil.ReadAll err %v", err)
	//	return "", err
	//}
	//
	//var authRespData AuthRespData
	//err = json.Unmarshal(body, &authRespData)
	//if err != nil {
	//	log.Errorf("【paypal】 json.Unmarshal(data, &authRespData) err:%v", err)
	//}
	//if paypalService.UnitTest == false {
	//	cacheKey := cache.GetPayPalRedisAccessToken()
	//	db.GetRedisClient().Set(context.Background(), cacheKey, authRespData.AccessToken, time.Duration(authRespData.ExpiresIn)*time.Second)
	//}
	//return authRespData.AccessToken, nil
	return "", nil
}
